<template>
  <div id="goodsPage">

    <el-row type="flex" justify="start" style="width: 100%; margin-bottom: 20px">
      <!-- 搜索区域 -->
      <el-col :span="24">
        <el-card>
          <el-form ref="searchFormComponents" :model="searchForm" inline label-position="left">
            <el-row :gutter="10" align="middle">
              <!-- 新增按钮 -->
              <el-col :span="3">
                <el-button
                    type="primary"
                    @click="showAddModal"
                    class="action-button"
                    style="margin-right: 15px"
                >
                  <i class="el-icon-plus"></i> 新增
                </el-button>
              </el-col>

              <!-- 搜索条件 -->
              <el-col :span="4">
                <el-form-item prop="name" label-width="40px">
                  <el-input
                      v-model="searchForm.name"
                      clearable
                      placeholder="货物名称"
                  ></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="4">
                <el-form-item prop="type" label-width="40px">
                  <el-input
                      v-model="searchForm.type"
                      clearable
                      placeholder="货物类型"
                  ></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="4">
                <el-form-item prop="code" label-width="40px">
                  <el-input
                      v-model="searchForm.code"
                      clearable
                      placeholder="货物批次"
                  ></el-input>
                </el-form-item>
              </el-col>

              <el-col :span="4">
                <el-form-item prop="state" label-width="40px">
                  <el-select
                      v-model="searchForm.state"
                      placeholder="全部状态"
                      clearable
                      style="width: 100%"
                  >
                    <el-option
                        v-for="item in stateList"
                        :key="item"
                        :label="item"
                        :value="item"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <!-- 按钮组 -->
              <el-col :span="5" class="search-button-col">
                <el-form-item>
                  <el-button
                      type="primary"
                      icon="el-icon-search"
                      @click="search"
                      style="margin-right: 10px"
                  >搜索</el-button>
                  <el-button
                      icon="el-icon-refresh"
                      @click="resetSearch"
                  >重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </el-col>
    </el-row>

    <!-- 表格区域 -->
    <el-card>
      <el-table
          :data="listData"
          style="width: 100%"
          v-loading="loading"
          tooltip-effect="dark"
          border
          :header-cell-style="{background: '#f5f7fa', color: '#606266'}"
      >
        <!-- 多选列 -->
        <el-table-column
            type="selection"
            width="55"
            align="center"
            fixed="left"
        ></el-table-column>

        <!-- 隐藏的货物ID列 -->
        <el-table-column
            prop="gid"
            label="货物ID"
            width="0"
            v-if="false"
            align="center"
        ></el-table-column>
        <!-- 表头 -->
        <!-- 序号列 -->
        <el-table-column
            type="index"
            label="序号"
            width="60"
            align="center"
        ></el-table-column>

        <!-- 货物名称 -->
        <el-table-column
            prop="name"
            label="货物名称"
            show-overflow-tooltip
            width="120"
            align="center"
        ></el-table-column>

        <!-- 货物类型 -->
        <el-table-column
            prop="type"
            label="货物类型"
            show-overflow-tooltip
            align="center"
            width="100"
        ></el-table-column>

        <!-- 单个货物面积 -->
        <el-table-column
            prop="area"
            label="单个面积"
            show-overflow-tooltip
            align="center"
            width="100"
        ></el-table-column>

        <!-- 单个货物价值 -->
        <el-table-column
            prop="money"
            label="单个价值"
            show-overflow-tooltip
            align="center"
            width="100"
        ></el-table-column>

        <!-- 实际货物数量 -->
        <el-table-column
            prop="nowNum"
            label="货物数量"
            show-overflow-tooltip
            align="center"
            width="100"
        ></el-table-column>

        <!-- 货物批次 -->
        <el-table-column
            prop="code"
            label="货物批次"
            show-overflow-tooltip
            align="center"
            width="120"
        ></el-table-column>

        <!-- 货物状态 -->
        <el-table-column
            label="货物状态"
            prop="state"
            show-overflow-tooltip
            align="center"
            width="100"
        ></el-table-column>

        <!-- 货物存放时长 -->
        <el-table-column
            prop="time"
            label="存放时长（天）"
            show-overflow-tooltip
            width="120"
            align="center"
        ></el-table-column>

        <!-- 操作列 -->
        <el-table-column
            fixed="right"
            label="操作"
            width="180"
            align="center"
        >
          <template slot-scope="scope">
            <el-button
                @click="handleEdit(scope.row)"
                type="primary"
                size="mini"
                class="action-button"
            >
              <i class="el-icon-edit"></i> 编辑
            </el-button>
            <el-button
                type="danger"
                @click="handleDelete(scope.row)"
                size="mini"
                class="action-button"
            >
              <i class="el-icon-delete"></i> 删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
            background
            layout="total, sizes, prev, pager, next, jumper"
            :current-page="searchForm.current"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="searchForm.pageSize"
            :total="searchForm.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </el-card>

    <!-- 新增货物 -->
    <el-dialog
        title="新增货物"
        :visible.sync="addDialogVisible"
        width="500px"
        custom-class="compact-dialog"
    >
      <el-form :model="addForm" ref="addForm" label-width="90px" class="compact-form">
        <!-- 名称类型 -->
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="货物名称" prop="name" class="required">
              <el-input v-model="addForm.name" placeholder="请输入名称" style="width: 130px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="货物类型" prop="type" class="required">
              <el-input v-model="addForm.type" placeholder="请输入类型" style="width: 130px"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 面积价值 -->
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="单个面积" prop="area">
              <el-input v-model.number="addForm.area" placeholder="㎡" suffix-icon="el-icon-square" style="width: 130px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单个价值" prop="money">
              <el-input v-model.number="addForm.money" placeholder="¥" suffix-icon="el-icon-money" style="width: 130px"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 数量 -->
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="入库数量" prop="initNum" class="required">
              <el-input-number
                  v-model="addForm.initNum"
                  :min="0"
                  placeholder="请填写数量"
                  controls-position="right"
                  style="width: 130px"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际数量" prop="nowNum" class="required">
              <el-input-number
                  v-model="addForm.nowNum"
                  :min="0"
                  placeholder="数量"
                  controls-position="right"
                  style="width: 130px"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 货物批次 -->
        <el-row :gutter="15">
          <el-col :span="24">
            <el-form-item label="货物批次" prop="code">
              <el-input v-model="addForm.code" placeholder="请输入批次号" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 区域编号 -->
        <el-row :gutter="15">
          <el-col :span="24">
            <el-form-item label="区域编号" prop="rCode">
              <el-input v-model="addForm.rCode" placeholder="区域编号" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 状态 -->
        <el-col :span="12">
          <el-form-item label="货物状态" prop="state" class="required">
            <el-select v-model="addForm.state" placeholder="请选择状态">
              <el-option
                  v-for="item in stateList"
                  :key="item"
                  :label="item"
                  :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <!-- 存放时长列 -->
        <el-col :span="12">
          <el-form-item label="存放时长" prop="time">
            <el-input-number
                v-model="addForm.time"
                :min="0"
                suffix-icon="el-icon-time"
                placeholder="天数"
                style="width: 100%"
            ></el-input-number>
          </el-form-item>
        </el-col>
      </el-form>

      <template #footer>
      <span class="dialog-footer">
        <el-button @click="addDialogVisible = false" class="cancel-btn">取 消</el-button>
        <el-button type="primary" @click="submitAddForm" class="confirm-btn">确 定</el-button>
      </span>
      </template>
    </el-dialog>

    <!-- 编辑货物信息 -->
    <el-dialog
        title="编辑货物"
        :visible.sync="editDialogVisible"
        width="500px"
        custom-class="compact-dialog"
    >
      <el-form
          :model="editForm"
          ref="editForm"
          label-width="90px"
          class="compact-form"
      >
        <!-- 名称类型 -->
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="货物名称" prop="name" class="required">
              <el-input v-model="editForm.name" placeholder="请输入名称" style="width: 130px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="货物类型" prop="type" class="required">
              <el-input v-model="editForm.type" placeholder="请输入类型" style="width: 130px"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 面积价值 -->
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="单个面积" prop="area">
              <el-input v-model.number="editForm.area" placeholder="㎡" suffix-icon="el-icon-square" style="width: 130px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单个价值" prop="money">
              <el-input v-model.number="editForm.money" placeholder="¥" suffix-icon="el-icon-money" style="width: 130px"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 数量 -->
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="入库数量" prop="initNum" class="required">
              <el-input-number
                  v-model="editForm.initNum"
                  :min="0"
                  placeholder="请填写数量"
                  controls-position="right"
                  style="width: 130px"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际数量" prop="nowNum" class="required">
              <el-input-number
                  v-model="editForm.nowNum"
                  :min="0"
                  placeholder="数量"
                  controls-position="right"
                  style="width: 130px"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 货物批次 -->
        <el-row :gutter="15">
          <el-col :span="24">
            <el-form-item label="货物批次" prop="code">
              <el-input v-model="editForm.code" placeholder="请输入批次号" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 区域编号 -->
        <el-row :gutter="15">
          <el-col :span="24">
            <el-form-item label="区域编号" prop="rCode">
              <el-input v-model="editForm.rCode" placeholder="区域编号" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 状态 -->
        <el-col :span="12">
          <el-form-item label="货物状态" prop="state" class="required">
            <el-select v-model="editForm.state" placeholder="请选择状态">
              <el-option
                  v-for="item in stateList"
                  :key="item"
                  :label="item"
                  :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <!-- 存放时长列 -->
        <el-col :span="12">
          <el-form-item label="存放时长" prop="time">
            <el-input-number
                v-model="editForm.time"
                :min="0"
                suffix-icon="el-icon-time"
                placeholder="天数"
                style="width: 100%"
            ></el-input-number>
          </el-form-item>
        </el-col>
      </el-form>

      <template #footer>
      <span class="dialog-footer">
        <el-button @click="editDialogVisible = false" class="cancel-btn">取 消</el-button>
        <el-button type="primary" @click="submitEditForm" class="confirm-btn">确 定</el-button>
      </span>
      </template>
    </el-dialog>

  </div>
</template>

<script>
import request from '@/utils/request';

export default {
  data() {
    return {
      // 表格数据
      listData: [],
      // 多选存储
      selectedItems: [],
      // 货物状态参数（在这修改状态
      stateList:  ['待入库', '待出库', '快逾期','逾期','已入库','已出库'],
      // 规则
      rules: {},
      // 查询参数
      searchForm: {
        name: '',  // 货物名称搜索
        type: '',  // 货物类型搜索
        code: '',  // 货物批次搜索
        state: '', // 货物状态搜索
        gid: '',
        // 分页参数
        current: 1,   // 当前页
        pageSize: 10, // 每页容量
        total: 0
      },
      // 添加货物参数
      addForm: {
        name: '',
        type: '',
        area: '',
        money: '',
        initNum: '',
        nowNum: '',
        code: '',
        state: '',
        time: '',
        rCode: '',
      },
      editForm: {
        gid: '',
        name: '',
        type: '',
        area: '',
        money: '',
        initNum: '',
        nowNum: '',
        code: '',
        state: '',
        time: '',
        rCode: '',
      },
      addDialogVisible: false,
      editDialogVisible: false,
      loading: false,
    }
  },
  mounted() {
    // 获取表格数据
    this.getGoodsInfo();
  },
  methods: {
    // 获取表格信息
    getGoodsInfo() {
      this.loading = true
      request.post('/goods/get/getGoods', {
        ...this.searchForm,
      })
          .then(response => {
            // 响应结果有个data属性，存的是接口返回的数据，所以多个data
            this.listData = response.data.data.data.list;
            this.searchForm.total = response.data.data.data.total;
            this.loading = false
          })
          .catch(error => {
            console.error('获取货物信息列表失败:', error);
            this.$message.error('获取货物信息列表失败');
            this.loading = false;
          });
    },
    // 添加货物
    submitAddForm() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          request.post(`/goods/addGoods`, this.addForm)
              .then(response => {
                if (response.data.code === "200") {
                  this.$message.success('添加成功');
                  this.addDialogVisible = false;
                  this.getGoodsInfo();
                } else {
                  this.$message.error('添加失败');
                }
              })
              .catch(error => {
                console.error('添加失败:', error);
                this.$message.error('添加失败');
              });
        }
      });
    },
    // 编辑货物
    handleEdit(row) {
      this.editForm = {
        ...row,
        // 如果后端需要数值类型转换
        area: Number(row.area),
        money: Number(row.money),
        initNum: Number(row.initNum),
        nowNum: Number(row.nowNum),
        time: Number(row.time)
      }
      this.editDialogVisible = true
    },

    // 提交编辑表单
    submitEditForm() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          request.post(`/goods/updateGoods`, this.editForm)
              .then(response => {
                if (response.data.code === "200") {
                  this.$message.success('修改成功')
                  this.editDialogVisible = false
                  this.getGoodsInfo()
                } else {
                  this.$message.error('修改失败')
                }
              })
              .catch(error => {
                console.error('修改失败:', error)
                this.$message.error('修改失败')
              })
        }
      })
    },
    // 查询
    search() {
      this.searchForm.current = 1;
      this.getGoodsInfo();
    },
    // 重置搜索框
    resetSearch() {
      // 重置表单字段
      this.$refs.searchFormComponents.resetFields();
      // 重置分页到第一页
      this.searchForm.current = 1;
      // 重新获取数据
      this.getGoodsInfo();
    },
    // 分页
    handleSizeChange(newSize) {
      this.searchForm.pageSize = newSize;
      this.searchForm.current = 1;
      this.getGoodsInfo();
    },
    handleCurrentChange(newPage) {
      this.searchForm.current = newPage;
      this.getGoodsInfo();
    },
    // 新增
    showAddModal() {
      this.addForm = {
        loginName: '',
        name: '',
        password: '',
        phone: '',
        rid: null
      };
      this.addDialogVisible = true;
    },
    // 删除
    handleDelete(row) {
      this.$confirm('是否删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        customClass: 'tech-message-box'
      }).then(() => {
        request.post('/goods/deleteGoods',{
          gid: row.gid,
        })
            .then(response => {
              if (response.data.code === "200") {
                this.$message.success('删除成功')
                this.getGoodsInfo()
              } else {
                this.$message.error('删除失败')
              }
            })
            .catch(error => {
              console.error('删除失败:', error);
              this.$message.error('删除失败');
            });
      }).catch(() => {});
    },
  }
}
</script>

<style scoped>
#goodsPage {
  padding: 20px;
}
</style>
